package com.woniu.Service.impl;

import com.woniu.Service.MenuService;
import com.woniu.entity.RoleMenu;
import com.woniu.mapper.MenuMapper;
import com.woniu.mapper.RoleMenuMapper;
import com.woniu.vo.MenuVo;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Author: Gwj
 * @Date: 2024/7/1 19:00
 */
@Service
@Transactional
public class MenuServiceImpl implements MenuService {
    @Resource
    private MenuMapper menuMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    /**
     * 个人登录之后的数据存在缓存redis里 避免重复查询数据库，设置过期时间30分钟
     *
     * @param accountName
     * @return
     * @throws Exception
     */
    @Override
    public List<MenuVo> selectMenuByAccountName(String accountName) throws Exception {
        Object temp = redisTemplate.opsForValue().get("menu" + accountName);
        if (temp != null) {
            return (List<MenuVo>) temp;
        }
        List<MenuVo> menuList = menuMapper.selectMenuByAccountName(accountName);
        redisTemplate.opsForValue().set("menu" + accountName, menuList);
        redisTemplate.expire("menu" + accountName, 15, TimeUnit.MINUTES);
        return menuList;
    }

    /**
     * 把用户权限设置在缓存中，并且设置过期时间，用于权限的判断
     *
     * @param accountName
     * @throws Exception
     */
    @Override
    public void selectMenuPermissionByAccountName(String accountName) throws Exception {
        //得到当前用户的权限，把权限设置到缓存里
        Set<String> set = stringRedisTemplate.opsForSet().members("permission" + accountName);
        if (set.isEmpty()) {
            List<String> list = menuMapper.selectMenuPermissionByAccountName(accountName);
            String[] ss = new String[list.size()];
            for (int i = 0; i < ss.length; i++) {
                ss[i] = list.get(i);
            }
            stringRedisTemplate.opsForSet().add("permission" + accountName, ss);
            //设置过期时间
            stringRedisTemplate.expire("permission" + accountName, 15, TimeUnit.MINUTES);
        }
    }

    /**
     * 查询所有的权限
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<MenuVo> selectAllPermission() throws Exception {
        return menuMapper.selectAllPermission();
    }

    /**
     * 获取与给定角色ID关联的菜单ID集合
     *
     * @param roleId
     * @return
     * @throws Exception
     */
    @Override
    public Set<Integer> getPermissionById(Integer roleId) throws Exception {
        Example example = new Example(RoleMenu.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("roleId", roleId);
        List<RoleMenu> list = roleMenuMapper.selectByExample(example);
        HashSet<Integer> set = new HashSet<>();
        for (RoleMenu role : list) {
            set.add(role.getMenuId());
        }
        return set;
    }
}
